package org.linlinjava.litemall.admin.web;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.linlinjava.litemall.admin.annotation.RequiresPermissionsDesc;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.core.validator.Order;
import org.linlinjava.litemall.core.validator.Sort;
import org.linlinjava.litemall.db.domain.LitemallDeliveryNotice;
import org.linlinjava.litemall.db.service.LitemallDeliveryNoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;

@RestController
@RequestMapping("/admin/delivery/notice")
@Validated
public class AdminDeliveryNoticeController {
    private final Log logger = LogFactory.getLog(AdminDeliveryNoticeController.class);

    @Autowired
    private LitemallDeliveryNoticeService deliveryNoticeService;

    /**
     * 查询配送须知列表
     *
     * @param page 页码
     * @param limit 每页数量
     * @param sort 排序字段
     * @param order 排序方式
     * @return 配送须知列表
     */
    @RequiresPermissions("admin:delivery:notice:list")
    @RequiresPermissionsDesc(menu = {"配置管理", "配送须知"}, button = "查询")
    @GetMapping("/list")
    public Object list(String title,
                      @RequestParam(defaultValue = "1") Integer page,
                      @RequestParam(defaultValue = "10") Integer limit,
                      @Sort @RequestParam(defaultValue = "add_time") String sort,
                      @Order @RequestParam(defaultValue = "desc") String order) {
        List<LitemallDeliveryNotice> notices = deliveryNoticeService.queryList();
        return ResponseUtil.okList(notices);
    }

    /**
     * 添加配送须知
     *
     * @param notice 配送须知信息
     * @return 添加结果
     */
    @RequiresPermissions("admin:delivery:notice:create")
    @RequiresPermissionsDesc(menu = {"配置管理", "配送须知"}, button = "添加")
    @PostMapping("/create")
    public Object create(@RequestBody LitemallDeliveryNotice notice) {
        deliveryNoticeService.add(notice);
        return ResponseUtil.ok(notice);
    }

    /**
     * 更新配送须知
     *
     * @param notice 配送须知信息
     * @return 更新结果
     */
    @RequiresPermissions("admin:delivery:notice:update")
    @RequiresPermissionsDesc(menu = {"配置管理", "配送须知"}, button = "编辑")
    @PostMapping("/update")
    public Object update(@RequestBody LitemallDeliveryNotice notice) {
        if (deliveryNoticeService.update(notice) == 0) {
            return ResponseUtil.updatedDataFailed();
        }
        return ResponseUtil.ok(notice);
    }

    /**
     * 删除配送须知
     *
     * @param notice 配送须知信息
     * @return 删除结果
     */
    @RequiresPermissions("admin:delivery:notice:delete")
    @RequiresPermissionsDesc(menu = {"配置管理", "配送须知"}, button = "删除")
    @PostMapping("/delete")
    public Object delete(@RequestBody LitemallDeliveryNotice notice) {
        Integer id = notice.getId();
        if (id == null) {
            return ResponseUtil.badArgument();
        }
        deliveryNoticeService.delete(id);
        return ResponseUtil.ok();
    }
} 